/*
 * LocalGPS.java
 *
 * Created on 13 de junio de 2008, 20:59
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package project;
import java.util.*;
import javax.microedition.location.*;

/**
 *
 * @author Ana_Pedro
 */
public class LocalGPS {
    LocationProvider locationProvider;
    long locationTS;
    /** Creates a new instance of LocalGPS */
    public void init() {
        try {
            locationTS=0L;
            locationProvider=LocationProvider.getInstance(null);
        } catch (Exception e) {
        }
    }
    
    public void close() {
        // locationProvider.reset();
        locationProvider=null;
    }
    
      public void getPosition(Position pos) {
        String nmeaData;
        int i=0;
        Location loc=null;

        try {
            loc=locationProvider.getLocation(1);
            locationProvider.reset();
        } catch (Exception e) {
            pos.valida=false;
            return;
        }
        if(loc.getTimestamp()!=locationTS) {
            locationTS=loc.getTimestamp();
        } else
            return;

        if(loc.isValid()) {
            int j=0, b1, b2, checksum;
            String linea;
            if((nmeaData=loc.getExtraInfo("application/X-jsr179-location-nmea"))!=null) {
                pos.nmea=true;
                do {
                    j=nmeaData.indexOf("*", i);
                    if(j!=-1)
                      linea=nmeaData.substring(i,j+3);
                    else
                        linea=nmeaData.substring(i);
                    i=j+1;
                    if((b1=linea.indexOf("$GPGGA"))!=-1) {                        
                        b2=linea.indexOf('*');
                        checksum=0;
                        for(int k=b1+1; k<b2; k++)
                            checksum^=linea.charAt(k);

                        try {
                            if(checksum==Integer.parseInt(linea.substring(b2+1),16))
                                ParserNMEA.parseGPGGA(linea, pos);                                                        
                        } catch(Exception e) {}                        
                    } else if((b1=linea.indexOf("$GPRMC"))!=-1) {
                        b2=linea.indexOf('*');
                        checksum=0;
                        for(int k=b1+1; k<b2; k++)
                            checksum^=linea.charAt(k);

                        try {
                            if(checksum==Integer.parseInt(linea.substring(b2+1),16))
                                ParserNMEA.parseGPRMC(linea, pos);
                        } catch(Exception e) {}                        
                    }
                    
                    if(j==-1)
                        break;
                } while(true);
            } else {
                float data;
                pos.nmea=false;
                Calendar timestamp=Calendar.getInstance();
                StringBuffer str=new StringBuffer(15);
                
                QualifiedCoordinates coord=loc.getQualifiedCoordinates();
                pos.altitud=coord.getAltitude();

                data=(float)coord.getLatitude();
                i=(int)data;
                pos.latitudNMEA=100.0f * i + (data - i) * 60.0f;
                
                data=(float)coord.getLongitude();
                i=(int)data;
                pos.longitudNMEA=100.0f * i + (data - i) * 60.0f;
                
                pos.rumbo=loc.getCourse();
                pos.velocidad=loc.getSpeed();

                timestamp.setTime(new Date(loc.getTimestamp()));
                i=timestamp.get(Calendar.DAY_OF_MONTH);
                if(i<10)
                    str.append("0");
                str.append(i);
                i=timestamp.get(Calendar.MONTH) - Calendar.JANUARY + 1;
                if(i<10)
                    str.append("0");
                str.append(i);
                i=timestamp.get(Calendar.YEAR);
                str.append(Integer.toString(i).substring(2));
                i=timestamp.get(Calendar.HOUR_OF_DAY);
                if(i<10)
                    str.append("0");
                str.append(i);
                i=timestamp.get(Calendar.MINUTE);
                if(i<10)
                    str.append("0");
                str.append(i);
                i=timestamp.get(Calendar.SECOND);
                if(i<10)
                    str.append("0");
                str.append(i);
                i=timestamp.get(Calendar.MILLISECOND);
                if(i<10)
                    str.append("0");
                if(i<100)
                    str.append("0");
                str.append(i);
                pos.timestamp=str.toString();
                
                pos.valida=true;
            }
        } else
            pos.valida=false;    
    }
}
